package cn.itcast.erp.service.impl;

import cn.itcast.erp.dao.mapper.MenuMapper;
import cn.itcast.erp.dao.mapper.RoleMapper;
import cn.itcast.erp.entity.EmpRole;
import cn.itcast.erp.entity.Menu;
import cn.itcast.erp.entity.RoleMenus;
import cn.itcast.erp.entity.Tree;
import cn.itcast.erp.service.MenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("menuService")
public class MenuServiceImpl implements MenuService {

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private MenuMapper mm;

    @Override
    public List<Menu> getMenu(Long uuid) {

        List<Menu> menus = getMenus2(uuid);
        return menus;
    }


    @Override
    public List<Tree> readRoleMenus(Long uuid) {
        // 当前角色所拥有的菜单权限
        List<Menu> userMenuList = this.mm.getUserMenu(uuid);

        ArrayList<String> arrayList = new ArrayList<>();
        for (Menu menu : userMenuList) {
            arrayList.add(menu.getMenuid());
        }

        // 所有的菜单
        List<Menu> allMenuList = this.mm.getAllMenu();

        // 最终要返回的树形结构 数据
        List<Tree> treeList = new ArrayList<Tree>();

        // 根
        Menu root = allMenuList.remove(0);

        if (userMenuList != null && !userMenuList.isEmpty()) {
            userMenuList.remove(0);
        }

        Tree tree1 = null;

        Iterator<Menu> iterator = allMenuList.iterator();
        while (iterator.hasNext()) {
            Menu next = iterator.next();
            if (next.getPid().equals("0")) {

                tree1 = new Tree();
                tree1.setId(next.getMenuid());
                tree1.setText(next.getMenuname());
                tree1.setChecked(false);
                treeList.add(tree1);
                iterator.remove();
            }
        }
        for (Tree tree : treeList) {
            for (Menu menu : allMenuList) {
                if (tree.getId().equals(menu.getPid())) {
                    tree1 = new Tree();
                    tree1.setId(menu.getMenuid());
                    tree1.setText(menu.getMenuname());

                    if (arrayList.contains(menu.getMenuid())) {
                        tree1.setChecked(true);
                    } else {
                        tree1.setChecked(false);
                    }
                    tree.getChildren().add(tree1);
                }
            }

        }

        return treeList;
    }

    @Override
    public int updateRoleMenus(Long uuid, String checkedStr) {

        int i1 = 0;
        int i = this.roleMapper.deleteRoleMenus(uuid);

        List<RoleMenus> roleMenusList = new ArrayList();
        Set<String> set = new HashSet();
        if (checkedStr.length() != 0) {

            checkedStr = checkedStr + ",0";
            String[] ids = checkedStr.split(",");

            for (String id : ids) {

                set.add(id);

                if (!id.equals("0")) {
                    set.add(String.valueOf(Integer.valueOf(id) / 100 * 100));
                }
            }

            for (String id : set) {
                RoleMenus roleMenus = new RoleMenus();
                roleMenus.setMenuuuid(id);
                roleMenus.setRoleuuid(uuid);
                roleMenusList.add(roleMenus);
//                i1 = this.roleMapper.insertRoleMenus(uuid, id);
            }
            i1 = this.roleMapper.insertRoleMenusTwo(roleMenusList);

        }


        if (i >= 0 && i1 >= 0) {
            return 1;
        } else {
            return 0;
        }

    }


    private List<Menu> getMenus2(Long uuid) {
        List<Menu> menus = this.mm.getMenu(uuid);


        Menu root = menus.remove(0);
        formatList2Tree(root, menus);
        return root.getMenus();
    }

    private void formatList2Tree(Menu root, List<Menu> menus) {
        if (menus == null || menus.size() == 0) {
            return;
        }
        root.setMenus(new ArrayList<Menu>());
        for (int i = 0; i < menus.size(); i++) {
            Menu child = menus.get(i);
            if (child.getPid().equals(root.getMenuid())) {
                root.getMenus().add(menus.remove(i--));
            }
        }
        for (Menu menu : root.getMenus()) {
            if (menus.size() > 0) {
                formatList2Tree(menu, menus);
            }
        }

    }


}
